// 积分首页查询
bangkeapp.factory('accountService', ['$http', '$q', 'url', "userinfoService", "$ionicLoading",
function($http, $q, url, userinfoService, $ionicLoading) {
    var redpacket = function() {
    		
    		var result = {};
    		
    		var data = userinfoService.getData();
    		var member = userinfoService.getUser();
		data.request.body.lstRedpacket = []
		data.request.body.lstRedpacket[0] = member;
		
        var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
       	// 查询红包积分
		$http({    
            method: "JSONP",    
            url: url + "/redpacket/query?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			if ("M" == response.Response.Header.ResponseCode 
					&& response.Response.Body.Response.length > 0) {
				
				if (member.memberid == response.Response.Body.Response[0].memberid) {
					result.redpacket = response.Response.Body.Response[0].redpacket;
					progress = "正确";
				} else {
					result.redpacket = 0;
					progress = "错误";
				}
			} else {
				result.redpacket = 0;
				progress = response.Response.Header.MsgList[0];
			}
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
			result.interests = 0;
		});
		return promise;
	}
    var bonus = function() {
    		
    		var result = {};
    		
    		var data = userinfoService.getData();
    		var member = userinfoService.getUser();
		data.request.body.lstBonus = []
		data.request.body.lstBonus[0] = member;
		
        var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
       	// 查询红包积分
		$http({    
            method: "JSONP",    
            url: url + "/bonus/query?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			if ("M" == response.Response.Header.ResponseCode 
				&& response.Response.Body.Response.length > 0) {
				
				if (member.memberid == response.Response.Body.Response[0].memberid) {
					result.bonus = response.Response.Body.Response[0].bonus;
					progress = "正确";
				} else {
					result.bonus = 0;
					progress = "错误";
				}
			} else {
				result.bonus = 0;
				progress = response.Response.Header.MsgList[0];
			}
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
			result.interests = 0;
		});
		return promise;
	}
    var interests = function() {
    		
    		var result = {};
    		
    		var data = userinfoService.getData();
    		var member = userinfoService.getUser();
		data.request.body.lstInterests = []
		data.request.body.lstInterests[0] = member;
		
        var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
       	// 查询红包积分
		$http({    
            method: "JSONP",    
            url: url + "/interests/query?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			if ("M" == response.Response.Header.ResponseCode 
				&& response.Response.Body.Response.length > 0) {
				
				if (member.memberid == response.Response.Body.Response[0].memberid) {
					result.interests = response.Response.Body.Response[0].interests;
					progress = "正确";
				} else {
					result.interests = 0;
					progress = "错误";
				}
			} else {
				result.interests = 0;
				progress = response.Response.Header.MsgList[0];
			}
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
			result.interests = 0;
		});
		return promise;
	}
    // 获取昨日返还信息
	var rebate = function() {
		var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
        var result = {};
		var member = userinfoService.getUser();
		
        var data = userinfoService.getData();
		data.request.body.member = []
		data.request.body.member[0] = member;
    		
		$http({    
            method: "JSONP",    
            url: url + "/rebate/query?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			if ("M" == response.Response.Header.ResponseCode) {
				result = response.Response.Body.Response[0];
				progress = true;
			} else {
				progress = false;
			}
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
		});
		return promise;
	}
	var advance = function() {
		var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
        var result = {};
		var member = userinfoService.getUser();
		
        var data = userinfoService.getData();
		data.request.body.lstAdvance = [];
		var obj = {};
		obj.memberid = member.memberid;
		data.request.body.lstAdvance[0] = obj;
    		
		$http({    
            method: "JSONP",    
            url: url + "/advance/query?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			if ("M" == response.Response.Header.ResponseCode) {
				result = response.Response.Body.Response[0];
				progress = true;
			} else {
				progress = false;
			}
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
		});
		return promise;
	}
	var baccount = function() {
		var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
        var result = {};
		var member = userinfoService.getUser();
		
        var data = userinfoService.getData();
		data.request.body.lstBaccount = [];
		var obj = {};
		obj.memberid = member.memberid;
		data.request.body.lstBaccount[0] = obj;
    		
		$http({    
            method: "JSONP",    
            url: url + "/baccount/query?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			if ("M" == response.Response.Header.ResponseCode) {
				result = response.Response.Body.Response[0];
				progress = true;
			} else {
				progress = false;
			}
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
		});
		return promise;
	}
	var taxation = function() {
		var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
        var result = {};
		var member = userinfoService.getUser();
		
        var data = userinfoService.getData();
		data.request.body.lstTaxation = [];
		var obj = {};
		obj.memberid = member.memberid;
		data.request.body.lstTaxation[0] = obj;
    		
		$http({    
            method: "JSONP",    
            url: url + "/baccount/query?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			if ("M" == response.Response.Header.ResponseCode) {
				result = response.Response.Body.Response[0];
				progress = true;
			} else {
				progress = false;
			}
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
		});
		return promise;
	}
	var account = function() {
		var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
        var result = {};
		var member = userinfoService.getUser();
		
        var data = userinfoService.getData();
		data.request.body.appAccountQuery = {};
		data.request.body.appAccountQuery.memberid = member.memberid;
    		
		$http({    
            method: "JSONP",    
            url: url + "/appaccount/query?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			if ("M" == response.Response.Header.ResponseCode) {
				result = response.Response.Body.Response;
				progress = true;
			} else {
				progress = false;
			}
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
		});
		return promise;
	};
	
   	var tarjectory = function(type){
   		var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
        var result = {};
        var data = userinfoService.getData();
		data.request.body.Trajectory = {};
		data.request.body.Trajectory.trajectorytype = type;
		data.request.body.Trajectory.memberid = userinfoService.getUser().memberid;
    		
		$http({    
            method: "JSONP",    
            url: url + "/tarjectory/querytrajectory?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			var result = response.Response.Body.trajectoryout;
			progress = "正确";
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
		});
		return promise;
   	}
    return {
		account : account,
		interests : interests,
		redpacket : redpacket,
		bonus: bonus,
        tarjectory: tarjectory
    };
}]);

// 会员
bangkeapp.factory('memberService', ['$http', '$q', 'url', "$ionicLoading", "userinfoService",
function($http, $q, url, $ionicLoading, userinfoService) {
	var myreference = function() {
		var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
        var result = {};
		var member = userinfoService.getUser();
		
        var data = userinfoService.getData();
		data.request.body.member = []
		data.request.body.member[0] = member;
    		
		$http({    
            method: "JSONP",    
            url: url + "/member/getmyreference?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			if ("M" == response.Response.Header.ResponseCode) {
				if (response.Response.Body.Response.length > 0){
					result = response.Response.Body.Response[0];
					progress = true;
				} else {
					progress = false;
				}
			} else {
				progress = false;
			}
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
		});
		return promise;
	}
	var getmemberinfo = function(membername) {
		var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
        var result = {};
		var member = {};
		member.membername = membername;
        var data = userinfoService.getData();
		data.request.body.member = []
		data.request.body.member[0] = member;
    		
		$http({    
            method: "JSONP",    
            url: url + "/member/getbymembername?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			if ("M" == response.Response.Header.ResponseCode) {
				result = response.Response.Body.Response;
				progress = true;
			} else {
				progress = false;
			}
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
		});
		return promise;
	}
	var savereference = function(reference){
		var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
        var result = {};
		var member = userinfoService.getUser();
		
        var data = userinfoService.getNewData();
		data.body.afmemberid = reference;
		data.body.bememberid = member.reference;
		data.body.memberid = member.memberid;
    		
		$http({    
            method: "JSONP",    
            // url: url + "/savereference/save?callback=JSON_CALLBACK",
            url: url + "/changerecommen/saveOrUpdate?callback=JSON_CALLBACK",
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			if ("M" == response.Header.ResponseCode) {
				result = response.body.recommender;
				progress = true;
			} else {
				progress = false;
			}
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
		});
		return promise;
	}
    var getmemberlist = function() {
        var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
        var result = {};
		var member = {};
		member = userinfoService.getUser();
        var data = userinfoService.getData();
		data.request.body.member = []
		data.request.body.member[0] = member;
    		
		$http({    
            method: "JSONP",    
            url: url + "/member/getmymemberlist?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			if ("M" == response.Response.Header.ResponseCode) {
				result = response.Response.Body.Response;
				progress = true;
			} else {
				progress = false;
			}
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
		});
		return promise;
	}
    var upgrademember = function(member) {
        var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
        var result = {};
        var data = userinfoService.getData();
		data.request.body.member = []
		data.request.body.member[0] = member;
    		
		$http({    
            method: "JSONP",    
            url: url + "/member/upgrademember?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			if ("M" == response.Response.Header.ResponseCode) {
				userinfoService.setUser(response.Response.Body.Response[0]);
				progress = true;
			} else {
				alert(response.Response.Header.MsgList[0].MsgContent);
				progress = false;
			}
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
		});
		return promise;
   };
   var redpacket = function() {
    		var result = {};
   		var data = userinfoService.getData();
    		var member = userinfoService.getUser();
		data.request.body.lstRedpacket = []
		data.request.body.lstRedpacket[0] = member;
		
        var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
       	// 查询红包积分
		$http({    
            method: "JSONP",    
            url: url + "/redpacket/query?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			if ("M" == response.Response.Header.ResponseCode 
					&& response.Response.Body.Response.length > 0) {
				
				if (member.memberid == response.Response.Body.Response[0].memberid) {
					result.redpacket = response.Response.Body.Response[0].redpacket;
					progress = "正确";
				} else {
					result.redpacket = 0;
					progress = "错误";
				}
			} else {
				result.redpacket = 0;
				progress = response.Response.Header.MsgList[0];
			}
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
			result.interests = 0;
		});
		return promise;
	};
	
	var sendredpacket = function(sendredpacket, page, pagesize) {
		
        var data = userinfoService.getData();
		data.request.body.page = page;
		data.request.body.pagesize = pagesize;
		data.request.body.SendRedPacket = []
		data.request.body.SendRedPacket[0] = sendredpacket;
		
		var deferred = $q.defer();
		var promise = deferred.promise;
		var progress;
		$http({    
            method: "JSONP",    
            url: url + "/sendredpacket/save?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			progress = "正确";
			deferred.notify(progress);
			deferred.resolve(response);
		})
		.error(function(error) {
			deferred.reject(error);
		});
		return promise;
	};

	var sendinterests = function(sendinterests, page, pagesize) {
		
        var data = userinfoService.getData();
		data.request.body.page = page;
		data.request.body.pagesize = pagesize;
		data.request.body.sendinterests = []
		data.request.body.sendinterests[0] = sendinterests;
		
		var deferred = $q.defer();
		var promise = deferred.promise;
		var progress;
		$http({    
            method: "JSONP",    
            url: url + "/sendinterests/save?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			var result = response;
			progress = "正确";
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
		});
		return promise;
	};
	
	var advandeinapplyupload = function(advandeinapply) {
        var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
        var result = {};
        var data = userinfoService.getData();
		data.request.body.lstAdvandeinapplyinfo = []
		data.request.body.lstAdvandeinapplyinfo[0] = advandeinapply;
    		
		$http({    
            method: "JSONP",    
            url: url + "/Advandeinapplyinfo/save?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			var result = response;
			progress = "正确";
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
		});
		return promise;
    };
	var issettedpaypwd = function() {
        var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
        var result = {};
        var data = userinfoService.getNewData();
		data.body.memberid = userinfoService.getUser().memberid;
    		
		$http({    
            method: "JSONP",    
            url: url + "/securepwdchange/selectIsSecurePWD?callback=JSON_CALLBACK",  
            
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			var result = response;
			progress = "正确";
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
		});
		return promise;
    };
    var getsmscode = function(tel) {
    	
    		var data = userinfoService.getData();
		data.request.body.smscode = {};
		data.request.body.smscode.tel = tel;
		
        var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
       
		$http({    
            method: "JSONP",    
            url: url + "/smscode/get?callback=JSON_CALLBACK",  
            params: {
				"body": angular.toJson(data)
			}
		}).success(function() {
		})
		.error(function(error) {
			deferred.reject(error);
		});
		return promise;
    }
    var savepaypwd = function(body){
        var deferred = $q.defer();
        var promise = deferred.promise;
        var progress;
        var result = {};
        var data = userinfoService.getNewData();
		data.body = body;
    		
		$http({    
            method: "JSONP",    
            url: url + "/securepwdchange/saveOrUpdateSecurePWD?callback=JSON_CALLBACK",  
            
            params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			
			var result = response;
			progress = "正确";
			deferred.notify(progress);
			deferred.resolve(result);
		})
		.error(function(error) {
			deferred.reject(error);
		});
		return promise;
    }
    //签到初始化
	var sign=function () {
		var deferred = $q.defer();
		var promise = deferred.promise;
		var progress;
		var data = userinfoService.getData().request;
		$http({
			method: "JSONP",
			url: url + "/Signin/queryIsSignin?callback=JSON_CALLBACK",
			params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			var result = response.body;
			progress = "正确";
			deferred.notify(progress);
			deferred.resolve(result);
		})
			.error(function(error) {
				deferred.reject(error);
			});
		return promise;
	};
	//点击签到
	var toSign=function () {
		var deferred = $q.defer();
		var promise = deferred.promise;
		var progress;
		var data = userinfoService.getData().request;
		$http({
			method: "JSONP",
			url: url + "/Signin/save?callback=JSON_CALLBACK",
			params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			var result = response.body;
			progress = "正确";
			deferred.notify(progress);
			deferred.resolve(result);
		})
			.error(function(error) {
				deferred.reject(error);
			});
		return promise;
	};
	//签到表
	var signList=function () {
		var deferred = $q.defer();
		var promise = deferred.promise;
		var progress;
		var data = userinfoService.getData().request;
		var now = new Date();
		var month=now.getMonth()+1;
		var newMonth='';
		if(month>10){
			newMonth=month
		}else {
			newMonth='0'+month
		}
		data.body.signdate=now.getFullYear() + "-" + newMonth;
		data.body.memberid=userinfoService.getUser().memberid;
		data.body.membername=userinfoService.getUser().membername;
		data.body.page='';
		data.body.pagesize='';
			$http({
			method: "JSONP",
			url: url + "/Signin/querySigninInfo?callback=JSON_CALLBACK",
			params: {
				"body": angular.toJson(data)
			}
		}).success(function(response) {
			var result = response.body;
			progress = "正确";
			deferred.notify(progress);
			deferred.resolve(result);
		})
			.error(function(error) {
				deferred.reject(error);
			});
		return promise;
	};
    return {
        upgrademember: upgrademember, // 升级会员
        redpacket: redpacket, // 红包积分查询
        memberlist: getmemberlist, // 获取我的会员
        myreference: myreference,// 获取推荐人信息
        getmemberinfo: getmemberinfo, // 获取用户信息
        savereference: savereference, // 修改推荐人
        sendredpacket: sendredpacket,// 发积分
        sendinterests: sendinterests, // 发股份
        advandeinapplyupload : advandeinapplyupload, // 预付款申请
        issettedpaypwd: issettedpaypwd, // 查询是否设置支付密码
        getsmscode : getsmscode, // 获取验证码
        savepaypwd : savepaypwd, // 保存支付密码
		sign:sign,//签到初始化
		toSign:toSign,//点击签到
		signList:signList//签到表
    };
}]);